package com.pxmeta.app.controller.user.utils;

import com.pxmeta.verify.entity.StuInfo;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.web.client.RestTemplate;

public class IdentificationUtils {
    public static StuInfo parseUrl(String vcode) {
        RestTemplate restTemplate = new RestTemplate();
        String url = "https://www.chsi.com.cn/xlcx/bg.do?vcode=" + vcode + "&srcid=bgcx";
        String html = restTemplate.getForObject(url, String.class);
        return parseHtml(html);
    }

    /**
     * 这里建议添加异常捕捉操作，
     * 这是一个爬虫方式来获取的，一旦网页发生变化，解析的部分没做非空检查，可能会抛出异常
     */
    private static StuInfo parseHtml(String strHtml) {
        Document doc = Jsoup.parse(strHtml, "UTF-8");
        Elements eleDiv2 = doc.getElementsByClass("tableTitle");
        if (eleDiv2.text().contains("学历")) {
            return parseXueLi(strHtml);
        } else if (eleDiv2.text().contains("学籍")) {
            return parseXueJi(strHtml);
        }
        return null;
    }

    /**
     * 学籍解析
     */
    private static StuInfo parseXueJi(String strHtml) {
        Document doc = Jsoup.parse(strHtml, "UTF-8");
        Elements eleDiv2 = doc.getElementsByClass("div2");
        if (eleDiv2 != null && !eleDiv2.isEmpty()) {
            Elements eleTd = eleDiv2.get(0).getElementsByTag("td");
            if (eleTd != null && !eleTd.isEmpty()) {
                StuInfo stuInfo = new StuInfo();
                // 姓名是图片，调用腾讯API实现ocr识别
                String nameImg = eleTd.get(1).getElementsByTag("img").get(0).attr("src");
//                stuInfo.setName(aiOcr(nameImg));
                stuInfo.setGender(eleTd.get(4).text());
                stuInfo.setIdCard(eleTd.get(6).text());
                stuInfo.setNation(eleTd.get(8).text());
                stuInfo.setBirthDay(eleTd.get(10).text());
                stuInfo.setUniversity(eleTd.get(12).text());
                stuInfo.setLevel(eleTd.get(14).text());
                stuInfo.setDepartment(eleTd.get(16).text());
                stuInfo.setSClass(eleTd.get(18).text());
                stuInfo.setDomain(eleTd.get(20).text());
                stuInfo.setStuNum(eleTd.get(22).text());
                stuInfo.setForm(eleTd.get(24).text());
                stuInfo.setEntranceDate(eleTd.get(26).text());
                stuInfo.setLenOfSchooling(eleTd.get(28).text());
                stuInfo.setType(eleTd.get(30).text());
                String[] status = eleTd.get(32).text().split("\\(");
                stuInfo.setStatus(status[0]);
                stuInfo.setGraduationDate(status[1].substring(0, status[1].length() - 1));
                return stuInfo;
            }
        }
        return null;
    }

    /**
     * 学历解析
     */
    private static StuInfo parseXueLi(String strHtml) {
        Document doc = Jsoup.parse(strHtml, "UTF-8");
        Elements eleDiv2 = doc.getElementsByClass("div2");
        if (eleDiv2 != null && !eleDiv2.isEmpty()) {
            Elements eleTd = eleDiv2.get(0).getElementsByTag("td");
            if (eleTd != null && !eleTd.isEmpty()) {
                StuInfo stuInfo = new StuInfo();
                // 姓名是图片，调用腾讯API实现ocr识别
                String nameImg = eleTd.get(0).getElementsByTag("img").get(0).attr("src");
//                stuInfo.setName(aiOcr(nameImg));
                stuInfo.setGender(eleTd.get(2).text());
                stuInfo.setBirthDay(eleTd.get(3).text());
                stuInfo.setEntranceDate(eleTd.get(4).text());
                stuInfo.setGraduationDate(eleTd.get(5).text());
                stuInfo.setType(eleTd.get(6).text());
                stuInfo.setLevel(eleTd.get(7).text());
                stuInfo.setUniversity(eleTd.get(8).text());
                stuInfo.setLenOfSchooling(eleTd.get(9).text());
                stuInfo.setDomain(eleTd.get(10).text());
                stuInfo.setForm(eleTd.get(11).text());
                stuInfo.setCertificateNum(eleTd.get(12).text());
                stuInfo.setStatus("已毕业"); //有学历了肯定已经毕业了...
                stuInfo.setPresident(eleTd.get(14).text());
                return stuInfo;
            }
        }
        return null;
    }

//    /**
//     * 这个地方可能需要调用多次，腾讯 ocr 并不是每次都会正确返回结果，有时候会报系统繁忙请稍后再试
//     */
//    private static String aiOcr(String imgUrl) {
//        String url = "https://www.chsi.com.cn" + imgUrl;
//        TAipOcr aipOcr = new TAipOcr(APP_ID, APP_KEY);
//        try {
//            String result = aipOcr.handWritingOcrByUrl(url);
//            // fastjson 解析的时候也可能报错
//            OcrResult ocrResult = JSONObject.parseObject(result, OcrResult.class);
//            return ocrResult.getData().getItem_list().get(0).getItemstring();
//        } catch (Exception e) {
//            System.out.println("ocr 识别出错");
//            return "";
//        }
//    }
}
